<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>ZDH 审批事件新增</title>
    <meta name="keywords" content="ZDH 审批事件新增">
    <meta name="description" content="ZDH 审批事件新增">

    <link rel="shortcut icon" href="img/favicon.ico">
    <link href="css/plugins/chosen/chosen.css" rel="stylesheet">
    <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">
    <link href="css/plugins/switchery/switchery.css" rel="stylesheet">
    <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="js/plugins/magicsuggest/magicsuggest.css" rel="stylesheet">

</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">

        <div class="row">

            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>审批事件中心</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="buttons.html#">
                                <i class="fa fa-eye"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="javascript:void(0);" onclick="getResourceDesc()">功能说明</a>
                                </li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <form class="form-horizontal m-t" id="signupForm">

                            <div class="form-group">
                                <label class="col-sm-3 control-label">产品code：</label>
                                <div class="col-sm-8">
                                    <select id="product_code" name="product_code"
                                            data-placeholder="产品code...."
                                            class="chosen-select form-control m-b" >
                                    </select>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label">节点code：</label>
                                <div class="col-sm-8">
                                    <select id="code" name="code"
                                            data-placeholder=""
                                            class="chosen-select form-control m-b" tabindex="2">
                                        <option value="" mytype="">空</option>
                                    </select>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label">审批事件code：</label>
                                <div class="col-sm-8">
                                    <input id="event_code" name="event_code" type="text"
                                           placeholder="审批事件code" class="form-control" aria-required="true"> </span>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label">审批事件说明：</label>
                                <div class="col-sm-8">
                                    <input id="event_context" name="event_context" type="text"
                                           placeholder="审批事件说明" class="form-control" aria-required="true"> </span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">跳过审批流用户：</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="skip_account" name="skip_account" placeholder="ZDH用户账号,多用户逗号分隔">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label">回调接口：</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="call_back" name="call_back" placeholder="http://ip:port/xxx">
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-sm-8 col-sm-offset-3">
                                    <button class="btn btn-primary" type="button" id="btn_up" οnsubmit='return false'>保存</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <script src="js/zdh_common.js"></script>
    <!-- 全局js -->
    <script src="js/jquery.min.js?v=2.1.4"></script>
    <script src="js/bootstrap.min.js?v=3.3.6"></script>

    <!-- 自定义js -->
    <script src="js/content.js?v=1.0.0"></script>

    <script type="text/javascript" src="js/plugins/magicsuggest/magicsuggest.js"></script>

    <!-- jQuery Validation plugin javascript-->
    <script src="js/plugins/validate/jquery.validate.min.js"></script>
    <script src="js/plugins/validate/messages_zh.min.js"></script>

    <!-- Switchery -->
    <script src="js/plugins/switchery/switchery.js"></script>

    <!-- layer javascript -->
    <script src="js/plugins/layer/layer.min.js"></script>

    <script src="js/plugins/chosen/chosen.jquery.js"></script>

    <script src="js/admin/dim_product_common.js"></script>

    <script>

        (function(document, window, $) {


            window.addressIdMagicSuggest = $('#skip_account').magicSuggest({
                width:200,
                queryParam: 'username',
                method: 'get',
                allowFreeEntries: true,
                autoSelect:true,
                valueField:"id",
                displayField:"name",
                resultAsString:true,
                selectionStacked: true,
                resultsField: "result",

                //下拉框数据的获得：
                data: server_context+'/user_names'
            });

            var url = location.search; //这一条语句获取了包括问号开始到参数的最后，不包括前面的路径
            var params = url.substr(1);//去掉问号
            var pa = params.split("&");
            var s = new Object();//定义一个全局变量-存储任务ID
            for (var i = 0; i < pa.length; i++) {
                s[pa[i].split("=")[0]] = unescape(pa[i].split("=")[1]);
            }

            $('#code').chosen();

            function init_code(product_code){
                $.ajax({
                    type: 'POST',
                    url: server_context+"/approval_config_list",
                    async:false,
                    dataType: 'json',
                    data: {"product_code": product_code},
                    //成功返回
                    success: function (data) {
                        if(data.code != "200"){
                            layer.msg(data.msg);
                            return ;
                        }
                        var str = '<option value=\"\" hassubinfo=\"true\">空</option>';
                        for (var i = 0; i < data.result.length; i++) {
                            str += '<option value=\"' + data.result[i].code + '\" hassubinfo=\"true\">' + data.result[i].code_name + '</option>';
                        }
                        $('#code').html(str);
                        $("#code").trigger("chosen:updated");
                        $('#code').chosen();
                    },
                    //处理完成
                    complete: function () {
                    },
                    //报错
                    error: function (data) {
                    }
                });
            }


            if(s.id != '-1'){
                $.ajax({
                    type: 'GET',
                    url: server_context+"/approval_event_detail",
                    dataType: 'json',
                    async:false,
                    data: "id="+s.id,
                    //发送数据前
                    beforeSend: function () {
                    },
                    //成功返回
                    success: function (data) {
                        if(data.code == "200"){

                            var product_code = data.result.product_code;
                            $('#product_code').val(product_code);
                            $("#product_code").trigger("chosen:updated");
                            init_code(product_code);
                            $('#code').val(data.result.code);
                            $("#code").trigger("chosen:updated");
                            $('#event_code').val(data.result.event_code);
                            $('#event_context').val(data.result.event_context);
                            var skip_account=data.result.skip_account;
                            window.addressIdMagicSuggest.setValue(skip_account.split(','));
                            $('#call_back').val(data.result.call_back);
                            layer.msg('查询成功');
                        }else{
                            layer.msg('查询失败,'+data.result);
                        }
                    },
                    //处理完成
                    complete: function () {
                        $("#btn_up").removeAttr('disabled');
                        console.info("complete")
                    },
                    //报错
                    error: function (data) {
                        $("#btn_up").removeAttr('disabled');
                        layer.msg(data.responseText);
                        console.info("error: " + data.responseText);
                    }
                });

                $('#btn_up').click(function () {

                    var skip_account_str = "&skip_account="+window.addressIdMagicSuggest.getValue();

                    $("[name='skip_account[]']").remove();
                    $.ajax({
                        type: 'POST',
                        url: server_context+"/approval_event_update",
                        dataType: 'json',
                        data: $("#signupForm").serialize()+"&id="+s.id+skip_account_str,
                        //发送数据前
                        beforeSend: function () {
                            // 禁用按钮防止重复提交
                            $("#btn_up").attr({disabled: "disabled"});
                        },
                        //成功返回
                        success: function (data) {
                            $("#btn_up").removeAttr('disabled');
                            if(data.code == "200"){
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            }else{
                                layer.msg('更新失败');
                            }
                        },
                        //处理完成
                        complete: function () {
                            $("#btn_up").removeAttr('disabled');
                            console.info("complete")
                        },
                        //报错
                        error: function (data) {
                            $("#btn_up").removeAttr('disabled');
                            layer.msg(data.responseText);
                            console.info("error: " + data.responseText);
                        }
                    });
                });

            }else{
                $('#btn_up').click(function () {

                    if($('#event_context').val() == ""){
                        layer.msg("事件说明不可为空");
                        return ;
                    }

                    if($('#event_code').val() == ""){
                        layer.msg("事件code不可为空");
                        return ;
                    }

                    var skip_account_str = "skip_account="+window.addressIdMagicSuggest.getValue();

                    $("[name='skip_account[]']").remove();

                    $.ajax({
                        type: 'POST',
                        url: server_context+"/approval_event_add?"+skip_account_str,
                        dataType: 'json',
                        data: $("#signupForm").serialize(),
                        //发送数据前
                        beforeSend: function () {
                            // 禁用按钮防止重复提交
                            $("#btn_up").attr({disabled: "disabled"});
                        },
                        //成功返回
                        success: function (data) {
                            $("#btn_up").removeAttr('disabled');
                            if(data.code == "200"){
                                layer.msg('更新成功', {
                                    time: 2000, //20s后自动关闭
                                    btn: ['明白了,5s后自动关闭']
                                },function () {
                                    parent.layer.closeAll();
                                });
                            }else{
                                layer.msg('更新失败');
                            }

                        },
                        //处理完成
                        complete: function () {
                            $("#btn_up").removeAttr('disabled');
                            console.info("complete")
                        },
                        //报错
                        error: function (data) {
                            $("#btn_up").removeAttr('disabled');
                            layer.msg(data.responseText);
                            console.info("error: " + data.responseText);
                        }
                    });

                })
            }



        })(document, window, jQuery);


    </script>

</body>



</html>
